﻿using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using JigLibX.Physics;
using JigLibX.Collision;
using Services;
using Utilities.Menu.UIElements;
using EnhancedTimer.Resources;
using Services.Menu;
using Utilities;

namespace EnhancedTimer
{
    class CreditsMenu : DrawableGameComponent
    {
        
        ArrayList<MyButton> elements;

        MyButton menu;       
        InputManager input_manager;
        SpriteBatch spriteBatch;
        StateManager StateManager;
        bool transition_complete;
        public CreditsMenu(Game game)
            : base(game)
        {
        }
        public override void Initialize()
        {
            spriteBatch = Game.Services.GetService(typeof(SpriteBatch)) as SpriteBatch;
            StateManager = Game.Services.GetService(typeof(AStateManager)) as StateManager;
            Game.Services.RemoveService(typeof(IInputManager));
            input_manager = new InputManager(Game);
            Game.Components.Add(input_manager);
            input_manager.OnTap += on_tap;
            base.Initialize();
        }


        int[] elements_y = { 0, 100, 300, 350, 400, 450, 500 };
        int menu_y = 680;
        protected override void LoadContent()
        {
            var logo = Game.Content.Load<Texture2D>("Menu/vst_logo");
            //var emptyTexture = Game.Content.Load<Texture2D>("emptyPixel");
            var font = Game.Content.Load<SpriteFont>("Fonts/SegoeWP");
            var titleFont = Game.Content.Load<SpriteFont>("Fonts/SegoeWP_Title");
            var box = Game.Content.Load<Texture2D>("bar_box");
            var color = new Color (8,141,217);
            elements = new ArrayList<MyButton>();
            elements.Add(new MyButton(new Rectangle(0, 800 + elements_y[0], 480, 100), Strings.Title, Color.White, titleFont));
            elements.Add(new MyButton(new Rectangle(Game.GraphicsDevice.Viewport.Width / 2 - logo.Width / 2,
                800 + elements_y[1], logo.Width, logo.Height),
                logo, Color.White));
            elements.Add(new MyButton(new Rectangle(100, 800 + elements_y[2], 280, 80), Strings.Developers, Color.White, font, TextPos.Center));
            elements.Add(new MyButton(new Rectangle(100, 800 + elements_y[3], 280, 80), "Francesco Boscariol", Color.White, font, TextPos.Center));
            elements.Add(new MyButton(new Rectangle(100, 800 + elements_y[4], 280, 80), "Giuseppe Maggiore", Color.White, font, TextPos.Center));
            elements.Add(new MyButton(new Rectangle(100, 800 + elements_y[5], 280, 80), "Andrea Gasparetto", Color.White, font, TextPos.Center));
            var version = Strings.Version + (StateManager.isTrial ? "Trial" : "1.0");
            elements.Add(new MyButton(new Rectangle(100, 800 + elements_y[6], 280, 80), version, Color.White, font, TextPos.Center));
            menu = new MyButton(new Rectangle(100, 800 + menu_y, 280, 80), Strings.Back, Color.White, font, TextPos.Center, box, Color.White);
            transition_complete = false;
            base.LoadContent();
        }
        int transition_velocity = 100;
       
        public override void Update(GameTime gameTime)
        {
            if (elements[0].rectangle.Y <= 20)
                transition_complete = true;
            if (!transition_complete)
            {
                
                var deltay = -(int)(transition_velocity * (float)gameTime.ElapsedGameTime.TotalSeconds)*2;
                
                menu.rectangle.Y += deltay;
                for (int i = 0; i < elements.Count; i++)
                    elements[i].rectangle.Y += deltay;
            }
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                //StateManager.SelectMainMenu();
                StateManager.Resume(false);
                return;
            } 
            base.Update(gameTime);
        }
        Color barColor = new Color(31, 31, 31);
        public override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.Black);
            spriteBatch.Begin();
            for (int i = 0; i < elements.Count; i++)
                elements[i].Draw(spriteBatch);
            menu.Draw(spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }
        //Point tp = new Point();
        void on_tap(Vector2 tap_position)
        {
            //if (!transition_complete) return;
            //tp.X = (int)tap_position.X;
            //tp.Y = (int)tap_position.Y;
            if (!transition_complete)
            {
                menu.rectangle.Y = menu_y;
                for (int i = 0; i < elements.Count; i++)
                    elements[i].rectangle.Y = elements_y[i];
                transition_complete = true;
            }
            else if (StaticUtilities.RectContainsVect(menu.rectangle, tap_position))
                //StateManager.SelectMainMenu();
                StateManager.Resume(false);
        }
    
    }
}
